import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.TitledBorder;

/**
 * 
 * @author Devin Wolanick
 * @Version 1.0
 * 
 * Option Menu for Program
 */
public class OptionMenu {
	
	/**
	 * Variables
	 */ 
	private ButtonGroup hitHoldGroup = new ButtonGroup(); 
	private ButtonGroup percentGroup = new ButtonGroup();
	private ButtonGroup playersGroup = new ButtonGroup();
	private ButtonGroup decksGroup   = new ButtonGroup();
	private int[] counterTypes = new int[7];
	private JButton startButton;
	private JFrame optionFrame;

	public OptionMenu() {
		//Frame
		JFrame optionFrame = new JFrame("BlackJack Options");
		optionFrame.setLocation(350, 300);
		this.optionFrame = optionFrame;
		optionFrame.setVisible(true);
		optionFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Container pane = optionFrame.getContentPane();		
		
		//Main Panel
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
		pane.add(mainPanel);
		
		//Add Counting Selection
		addCountingSystem(mainPanel);
		
		//Add Bottom Panel
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new FlowLayout());
		mainPanel.add(bottomPanel);
		
		//Add H7S7 Panel and percentPanel
		addH7S7(bottomPanel);
		
		//Add Players Panel
		addPlayersPanel(bottomPanel);
		
		//Add Decks Panel
		addDecksPanel(bottomPanel);
		
		//Finalize
		optionFrame.pack();
		optionFrame.setResizable(false);
	}
	
	private void addDecksPanel(JPanel myPanel) {
		
		JPanel decksPanel = new JPanel();
		decksPanel.setLayout(new BoxLayout(decksPanel, BoxLayout.Y_AXIS));
		decksPanel.setPreferredSize(new Dimension(200,230));
		decksPanel.setMaximumSize(new Dimension(200,230));
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Select Number of Decks");
		decksPanel.setBorder(title);
		
		JRadioButton decks1 = new JRadioButton("1 Decks", false);
		JRadioButton decks2 = new JRadioButton("2 Decks", false);
		JRadioButton decks3 = new JRadioButton("3 Decks", false);
		JRadioButton decks4 = new JRadioButton("4 Decks", false);
		JRadioButton decks5 = new JRadioButton("5 Decks", false);
		JRadioButton decks6 = new JRadioButton("6 Decks", false);
		JRadioButton decks7 = new JRadioButton("7 Decks", false);
		JRadioButton decks8 = new JRadioButton("8 Decks", false);
		decks4.setSelected(true);
		
		decks1.setActionCommand("1");
		decks2.setActionCommand("2");
		decks3.setActionCommand("3");
		decks4.setActionCommand("4");
		decks5.setActionCommand("5");
		decks6.setActionCommand("6");
		decks7.setActionCommand("7");
		decks8.setActionCommand("8");
		
		this.decksGroup.add(decks1);
		this.decksGroup.add(decks2);
		this.decksGroup.add(decks3);
		this.decksGroup.add(decks4);
		this.decksGroup.add(decks5);
		this.decksGroup.add(decks6);
		this.decksGroup.add(decks7);
		this.decksGroup.add(decks8);
		
		decksPanel.add(decks1);
		decksPanel.add(decks2);
		decksPanel.add(decks3);
		decksPanel.add(decks4);
		decksPanel.add(decks5);
		decksPanel.add(decks6);
		decksPanel.add(decks7);
		decksPanel.add(decks8);
		myPanel.add(decksPanel);
	}
	
	/**
	 * Adds Number Of Players Panel
	 * @param myPanel Panel to add to
	 */
	private void addPlayersPanel(JPanel myPanel) {
		
		JPanel playersPanel = new JPanel();
		playersPanel.setLayout(new BoxLayout(playersPanel, BoxLayout.Y_AXIS));
		playersPanel.setPreferredSize(new Dimension(200,230));
		playersPanel.setMaximumSize(new Dimension(200,230));
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Select Number of Players");
		playersPanel.setBorder(title);
		
		JRadioButton players1 = new JRadioButton("1 Player", false);
		JRadioButton players2 = new JRadioButton("2 Players", false);
		JRadioButton players3 = new JRadioButton("3 Players", false);
		JRadioButton players4 = new JRadioButton("4 Players", false);
		JRadioButton players5 = new JRadioButton("5 Players", false);
		JRadioButton players6 = new JRadioButton("6 Players", false);
		JRadioButton players7 = new JRadioButton("7 Players", false);
		JRadioButton players8 = new JRadioButton("8 Players", false);
		players4.setSelected(true);
		
		players1.setActionCommand("1");
		players2.setActionCommand("2");
		players3.setActionCommand("3");
		players4.setActionCommand("4");
		players5.setActionCommand("5");
		players6.setActionCommand("6");
		players7.setActionCommand("7");
		players8.setActionCommand("8");
		
		this.playersGroup.add(players1);
		this.playersGroup.add(players2);
		this.playersGroup.add(players3);
		this.playersGroup.add(players4);
		this.playersGroup.add(players5);
		this.playersGroup.add(players6);
		this.playersGroup.add(players7);
		this.playersGroup.add(players8);
		
		playersPanel.add(players1);
		playersPanel.add(players2);
		playersPanel.add(players3);
		playersPanel.add(players4);
		playersPanel.add(players5);
		playersPanel.add(players6);
		playersPanel.add(players7);
		playersPanel.add(players8);
		myPanel.add(playersPanel);
	}
	
	/**
	 * Adds H7S7 Panel
	 * @param myPanel Panel to add to
	 */
	private void addH7S7(JPanel myPanel) {
		
		//H7S7 Panel
		JPanel holdHitPanel = new JPanel();
		holdHitPanel.setPreferredSize(new Dimension(100,75));
		holdHitPanel.setMaximumSize(new Dimension(100,75));
		holdHitPanel.setMinimumSize(new Dimension(100,75));
		holdHitPanel.setLayout(new BoxLayout(holdHitPanel, BoxLayout.Y_AXIS));
		TitledBorder title;
		title = BorderFactory.createTitledBorder("H17/S17");
		holdHitPanel.setBorder(title);
		JRadioButton radioH7 = new JRadioButton("H17", false);
		JRadioButton radioS7 = new JRadioButton("S17", false);
		
		radioS7.setSelected(true);
		
		radioS7.setActionCommand("0");
		radioH7.setActionCommand("1");
		this.hitHoldGroup.add(radioS7);
	    this.hitHoldGroup.add(radioH7);
	    holdHitPanel.add(radioS7);
		holdHitPanel.add(radioH7);
		
		
		//Percent Panel
		JPanel percentPanel = new JPanel();
		percentPanel.setPreferredSize(new Dimension(100,120));
		percentPanel.setMaximumSize(new Dimension(100,120));
		percentPanel.setMinimumSize(new Dimension(100,120));
		percentPanel.setLayout(new BoxLayout(percentPanel, BoxLayout.Y_AXIS));
		title = BorderFactory.createTitledBorder("%Re Shoe");
		percentPanel.setBorder(title);
		JRadioButton radio10 = new JRadioButton("10%", false);
		JRadioButton radio15 = new JRadioButton("15%", false);
		JRadioButton radio20 = new JRadioButton("20%", false);
		JRadioButton radio25 = new JRadioButton("25%", false);
		radio15.setSelected(true);
		
		radio10.setActionCommand("10");
		radio15.setActionCommand("15");
		radio20.setActionCommand("20");
		radio25.setActionCommand("25");
		this.percentGroup.add(radio10);
		this.percentGroup.add(radio15);
		this.percentGroup.add(radio20);
		this.percentGroup.add(radio25);
		percentPanel.add(radio10);
		percentPanel.add(radio15);
		percentPanel.add(radio20);
		percentPanel.add(radio25);
		
		//Combine
		JPanel smallSelect = new JPanel();
		smallSelect.setLayout(new BoxLayout(smallSelect, BoxLayout.Y_AXIS));
		JButton startButton = new JButton("Start");
		this.startButton = startButton;
		smallSelect.setPreferredSize(new Dimension(80,230));
		smallSelect.add(holdHitPanel);
		smallSelect.add(percentPanel);
		JPanel startPanel = new JPanel();
		startPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		startPanel.setPreferredSize(new Dimension(100,50));
		startPanel.setMaximumSize(new Dimension(100,50));
		startPanel.setMinimumSize(new Dimension(100,50));
		startPanel.add(startButton);
		smallSelect.add(startPanel);
		myPanel.add(smallSelect);
	}
	
	/**
	 * Adds Count Selection Panel
	 * @param myPanel Panel to add to
	 */
	private void addCountingSystem(JPanel myPanel) {
		
		JPanel countTypePanel = new JPanel();
		TitledBorder title;
		title = BorderFactory.createTitledBorder("Select Counting Systems");
		countTypePanel.setBorder(title);
		countTypePanel.setLayout(new FlowLayout());
		countTypePanel.setPreferredSize(new Dimension(470,60));
		
		JCheckBox checkBoxHiLo = new JCheckBox("Hi-Lo", false);
		JCheckBox checkBoxHiOpI = new JCheckBox("Hi-Opi", false);
		JCheckBox checkBoxHiOpII = new JCheckBox("Hi-Opii", false);
		JCheckBox checkBoxZen = new JCheckBox("Zen", false);
		JCheckBox checkBoxOmegaII = new JCheckBox("Omegaii", false);
		JCheckBox checkBoxKO = new JCheckBox("KO", false);
		JCheckBox checkBoxWizardA5 = new JCheckBox("Wizard-A5", false);
		
		checkBoxHiLo.setActionCommand("0");
		checkBoxHiOpI.setActionCommand("1");
		checkBoxHiOpII.setActionCommand("2");
		checkBoxZen.setActionCommand("3");
		checkBoxOmegaII.setActionCommand("4");
		checkBoxKO.setActionCommand("5");
		checkBoxWizardA5.setActionCommand("6");
		
		checkBoxHiLo.addActionListener(new CheckBoxListener());
		checkBoxHiOpI.addActionListener(new CheckBoxListener());
		checkBoxHiOpII.addActionListener(new CheckBoxListener());
		checkBoxZen.addActionListener(new CheckBoxListener());
		checkBoxOmegaII.addActionListener(new CheckBoxListener());
		checkBoxKO.addActionListener(new CheckBoxListener());
		checkBoxWizardA5.addActionListener(new CheckBoxListener());
		
		countTypePanel.add(checkBoxHiLo);
		countTypePanel.add(checkBoxHiOpI);
		countTypePanel.add(checkBoxHiOpII);
		countTypePanel.add(checkBoxZen);
		countTypePanel.add(checkBoxOmegaII);
		countTypePanel.add(checkBoxKO);
		countTypePanel.add(checkBoxWizardA5);
		myPanel.add(countTypePanel);
	}
	
	/**
	 * Adds Start Button Listener
	 * @param e
	 */
	public void addListener (ActionListener e) {
		this.startButton.addActionListener(e);
	}
	
	/**
	 * Closes Window
	 */
	public void closeWindow() {
		optionFrame.dispose();
	}
	
	public int[] getOptions() {
		int[] optionList = new int[11];
		//Dealer Hit Type
		optionList[0] = Integer.parseInt(hitHoldGroup.getSelection().getActionCommand());
		//Percent reShoe
		optionList[1] = Integer.parseInt(percentGroup.getSelection().getActionCommand());
		//Number of Players
		optionList[2] = Integer.parseInt(playersGroup.getSelection().getActionCommand());
		//Number of Decks
		optionList[3] = Integer.parseInt(decksGroup.getSelection().getActionCommand());
		//Counts On/Off
		optionList[4]  = counterTypes[0];
		optionList[5]  = counterTypes[1];
		optionList[6]  = counterTypes[2];
		optionList[7]  = counterTypes[3];
		optionList[8]  = counterTypes[4];
		optionList[9]  = counterTypes[5];
		optionList[10] = counterTypes[6];
		return optionList;
	}
	
	/**
	 * 
	 * @author Devin Wolanick
	 * @Version 1.0
	 * 
	 * CheckBox Listener for Counting Types
	 */
	class CheckBoxListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
        	int someCounter = Integer.parseInt(e.getActionCommand());
        	if (counterTypes[someCounter] == 1)
        		counterTypes[someCounter] = 0;
        	else if (counterTypes[someCounter] == 0)
        		counterTypes[someCounter] = 1;
        }
	}
}
